package com.gmail.blackdog1987.onedollar.util;

import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.gmail.blackdog1987.onedollar.entity.ResultJson;
import com.gmail.blackdog1987.onedollar.entity.base.Page;

public class JsonChangeUtil {

	/**
	 * 
	 * @param code
	 * @param message
	 * @return
	 */
	public static ResultJson setResultJson(String code, String message) {
		ResultJson resultJson = new ResultJson();
		resultJson.setCode(code);
		resultJson.setMessage(message);
		return resultJson;
	}

	/**
	 * 
	 * @return
	 */
	public static ResultJson setResultJson(Object bean) {
		ResultJson resultJson = new ResultJson();

		Class type = bean.getClass();
		Map data = new HashMap();
		BeanInfo beanInfo;
		try {
			beanInfo = Introspector.getBeanInfo(type);

			PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
			for (int i = 0; i < propertyDescriptors.length; i++) {
				PropertyDescriptor descriptor = propertyDescriptors[i];
				String propertyName = descriptor.getName();
				if (!propertyName.equals("class")) {
					Method readMethod = descriptor.getReadMethod();
					Object result = readMethod.invoke(bean, new Object[0]);
					if (result != null) {
						data.put(propertyName, result);
					} else {
						data.put(propertyName, "");
					}
				}
			}
		} catch (IntrospectionException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			e.printStackTrace();
		}
		resultJson.put("data", data);
		resultJson.setCode("0000");
		resultJson.setMessage("请求成功");

		return resultJson;
	}
	
	public static ResultJson setResultJSONList(Page page){
		ResultJson resultJson = new ResultJson();
		resultJson.put("currentPageNo", page.getCurrentPageNo());
		resultJson.put("firstPage", page.isFirstPage());
		resultJson.put("lastPage", page.isLastPage());
		resultJson.put("lastPageNo", page.getLastPageNo());
		resultJson.put("nextPage", page.getNextPage());
		resultJson.put("pageSize", page.getPageSize());
		resultJson.put("prePage", page.getPrePage());
		resultJson.put("totalCount", page.getTotalCount());
		
		// data
		List tmp = page.getDatas();
		List<Map> datas = new ArrayList<Map>();
		if(tmp!=null&&!tmp.isEmpty()){
			for (Object t : tmp) {
				BeanInfo beanInfo;
				try {
					beanInfo = Introspector.getBeanInfo(t.getClass());
					PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
					Map mp = new HashMap();
					for (int i = 0; i < propertyDescriptors.length; i++) {
						
						PropertyDescriptor descriptor = propertyDescriptors[i];
						String propertyName = descriptor.getName();
						if (!propertyName.equals("class")) {
							Method readMethod = descriptor.getReadMethod();
							Object result = readMethod.invoke(t, new Object[0]);
							if (result != null) {
								mp.put(propertyName, result);
							} else {
								mp.put(propertyName, "");
							}
						}
					}
					datas.add(mp);
				} catch (IntrospectionException e) {
					e.printStackTrace();
				} catch (IllegalAccessException e) {
					e.printStackTrace();
				} catch (IllegalArgumentException e) {
					e.printStackTrace();
				} catch (InvocationTargetException e) {
					e.printStackTrace();
				}
			}
		}
		
		resultJson.put("data", datas);
		
		resultJson.setCode("0000");
		resultJson.setMessage("请求成功");
		
		return resultJson;
	}

	public static ResultJson setResultJSONList(List list){
		ResultJson resultJson = new ResultJson();
		List<Map> datas = new ArrayList<Map>();
		if(list!=null&&!list.isEmpty()){
			for (Object t : list) {
				BeanInfo beanInfo;
				try {
					beanInfo = Introspector.getBeanInfo(t.getClass());
					PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
					Map mp = new HashMap();
					for (int i = 0; i < propertyDescriptors.length; i++) {

						PropertyDescriptor descriptor = propertyDescriptors[i];
						String propertyName = descriptor.getName();
						if (!propertyName.equals("class")) {
							Method readMethod = descriptor.getReadMethod();
							Object result = readMethod.invoke(t, new Object[0]);
							if (result != null) {
								mp.put(propertyName, result);
							} else {
								mp.put(propertyName, "");
							}
						}
					}
					datas.add(mp);
				} catch (IntrospectionException e) {
					e.printStackTrace();
				} catch (IllegalAccessException e) {
					e.printStackTrace();
				} catch (IllegalArgumentException e) {
					e.printStackTrace();
				} catch (InvocationTargetException e) {
					e.printStackTrace();
				}
			}
		}

		resultJson.put("data", datas);

		resultJson.setCode("0000");
		resultJson.setMessage("请求成功");

		return resultJson;
	}
}
